﻿
@{
    ViewBag.Title = "RequisitionView";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>库存调拨情况</legend>
</fieldset>
<div>
    <form class="layui-form" id="FormInfo">
        <div class="layui-form-item">
            <label class="layui-form-label">目标项目<br/>选择</label>
            <div class="layui-input-inline" style="width:400px;">
                <select name="ProjectId" lay-verify="required" lay-search lay-filter="ProjectIdSelFilter">
                    <option value=""></option>
                    @for (int i = 0; i < ViewBag.ProjectList.Count; i++)
                    {
                        <option value="@ViewBag.ProjectList[i]["ProjectId"]">@ViewBag.ProjectList[i]["Name"]|@ViewBag.ProjectList[i]["ProjectCode"]|@ViewBag.ProjectList[i]["Type"]</option>
                    }
                </select>
            </div>
            <label class="layui-form-label">库存地址</label>
            <div class="layui-input-inline" style="width:150px;">
                <select name="InvenAddressId" lay-verify="required" required lay-filter="InvenAddressSelFilter">
                    <option value=""></option>
                    <option value="1">-3F库房</option>
                    <option value="2">游泳池库房</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">搜索</label>
            <div class="layui-input-block">
                <input type="text" id="SearchText" name="Title1" required lay-verify="required" onkeypress="getKey()" placeholder="规格名称或材料编码" class="layui-input" style="width:300px;float:left;">
                <input type="text" value="" style="display:none" />
                <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="SearchFunc()"><i class="layui-icon">&#xe615;</i></button>
                <div class="layui-form-mid layui-word-aux">高级搜索</div>
            </div>
        </div>

    </form>
    <table id="demo" lay-filter="StorageTableList"></table>
</div>


<script type="text/javascript">


    var ProjectId = ""
    var InvenAddressId = "";
    var table;
    //注意：选项卡 依赖 element 模块，否则无法进行功能性操作
    layui.use(['form', 'table', 'upload', 'layer', 'element', 'laydate'], function ()
    {
        var element = layui.element;
        var upload = layui.upload;
        table = layui.table;
        var tab = parent.tab;
        var layer = layui.layer;
        var form = layui.form;
        var laydate = layui.laydate;

        var IsValidate = 0;
        var LayerIndex;
        var LayerRequi;
        element.init();

        form.render();

        form.on('select(ProjectIdSelFilter)', function (data)
        {
            //console.log(data.elem); //得到select原始DOM对象
            //console.log(data.value); //得到被选中的值
            //console.log(data.othis); //得到美化后的DOM对象
            ProjectId = data.value;
        });

        form.on('select(InvenAddressSelFilter)', function (data)
        {
            //console.log(data.elem); //得到select原始DOM对象
            //console.log(data.value); //得到被选中的值
            //console.log(data.othis); //得到美化后的DOM对象
            InvenAddressId = data.value;
        });

        form.on('submit(LayerRequiFromSubmit)', function (data)
        {
            //layer.msg(JSON.stringify(data.field));
            IsValidate = 1;
            return false;
        });


        $("body").on("click", '#LayerRequiBtnSave', function ()
        {
            var ReAmount = Number($("#LayerRequiFromRequiAmount").val());
            var RemaAmount = Number($("#LayerRequiFromRemaAmount").text());
            if (ReAmount > RemaAmount || ReAmount <= 0) {
                layer.msg('调拨数量有误', { icon: 6, time: 1000 });
                return;
            }



            $("#LayerRequiFromSubmit").click();
            if (IsValidate == 1) {
                IsValidate = 0;
                var obj = $("#RequiFrom").serializeArray();
                var data = {};
                $.each(obj, function (i, v)
                {
                    data[v.name] = v.value;
                });

                //alert(JSON.stringify(data));
                //更新退货库存
                $.ajax({
                    type: "POST",
                    url: "/Inventory/InvenRequisition",
                    async: false,
                    contentType: "application/json",
                    dataType: "json",
                    data: JSON.stringify(data),
                    success: function (msg)
                    {
                        if (msg.result == "OK") {
                            layer.msg('调拨成功', { icon: 6, time: 1000 });
                            table.reload("demo");
                            layer.close(LayerRequi);
                            //parent.layui.admin.events.closeThisTabs();
                        } else {
                            alert(msg.result);
                        }
                    }
                });
                //alert(JSON.stringify(data));
            }
        });


        table.render({
            elem: '#demo'
            , method: 'post'
            //, height: 315
            , url: '/Inventory/RequisitionTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "a.InvenRequisitionID", Order: "desc", Condition: " and 1=0" }
            , limit: 12
            , limits: [12, 50, 100]
            , cols: [[ //表头
                { field: 'Code', title: '物料编码', width: 110 }
                , { field: 'Specification', title: '规格名称', width: 300 }
                , { field: 'Unit', title: '单位', width: 70 }
                , { field: 'RequiAmount', title: '调拨数量', width: 110 }
                , { field: 'SingPrice', title: '市场单价', width: 110 }
                , {
                    title: '总价', width: 110,
                    templet: function (d)
                    {
                        return (d.RequiAmount * d.SingPrice).toFixed(2);
                    }
                }
                , { field: 'InProjName', title: '原项目名', width: 200 }
                , { field: 'OriAdress', title: '原存放地址', width: 150 }
                , { field: 'OutProjName', title: '目标项目名', width: 200 }
                //, {
                //    title: "操作&nbsp<br>&nbsp", width: 100, fixed: 'right', align: 'center',
                //    templet: function (d)
                //    {
                //        return '<a class="layui-btn layui-btn-xs" lay-event="Requisition">调拨</a>';
                //    }
                //}
            ]]
        });


        table.on("tool(StorageTableList)", function (obj)
        {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'Requisition') {
                LayerRequi = layer.open({
                    type: 1,
                    title: ['调拨操作', 'font-size:18px;'],
                    area: ['900px', '400px'],
                    content: $("#LayerRequiFrom").html(),  //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
                form.render();
                $("#LayerRequiFromCode").text(data.Code);
                $("#LayerRequiFromSpecification").text(data.Specification);
                $("#LayerRequiFromRemaAmount").text(data.storNum);

                $("#RequiFromInventoryId").val(data.InventoryId);




                laydate.render({
                    elem: '#RequiFromRequiTime' //指定元素
                });
                form.render();

            }


            if (layEvent == 'Detail') {
                layer.open({
                    type: 2,
                    title: ['新增库存条目', 'font-size:18px;'],
                    area: ['900px', '600px'],
                    content: '/Inventory/InveEditView?InventoryId=' + data.InventoryId  //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });

            }


            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除“物料编码：' + data.Code + '，规格名称:' + data.Specification + '”？', {
                    btn: ['确定删除', '取消'] //可以无限个按钮
                    , btn1: function (index)
                    {
                        //按钮【删除】的回调
                        //SSEditDelete
                        $.ajax({
                            type: "POST",
                            url: "/Inventory/InveDel",
                            async: false,
                            data: { "InventoryId": data.InventoryId },
                            success: function (msg)
                            {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功"', { icon: 6, time: 1000 });
                                    table.reload("demo", {
                                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                                    });
                                    //tab.close(tab.getId());
                                } else {
                                    alert("重置失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index)
                    {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }

        });


    });

    //回车键
    function getKey()
    {
        if (event.keyCode == 13) {
            SearchFunc();
        }
    }

    function SearchFunc()
    {

        var text = $("#SearchText").val();

        var condition = "";
        if (ProjectId != "") {
            condition += " and a.TargProjId=" + ProjectId;
        }
        if (InvenAddressId != "") {
            condition += " and b.InvenAddressId=" + InvenAddressId;
        }
        if (text == "") {
            table.reload("demo", {
                where: {
                    SortName: "a.InvenRequisitionID", Order: "desc", Condition: " and 1=1 " + condition
                },
                page: {
                    curr: 1
                }
            });
        }
        else {
            table.reload("demo", {
                where: {
                    SortName: "a.InvenRequisitionID", Order: "desc", Condition: " and (b.Specification like '%" + text + "%' or b.Code like '%" + text + "%') " +
                        condition + ""
                },
                page: {
                    curr: 1
                }
            });
        }
    }

</script>

